

import worker from '@ohos.worker'
import filemanager from '@ohos.filemanager'


var TAG = 'DataWorker'
const parentPort = worker.parentPort
parentPort.onmessage = function (e) {
    let data = e.data
    if (data.request_data == 'getRoot') {
        getRootData(data)
    } else {
        getListFile(data)
    }
}

parentPort.onmessageerror = function () {
    console.log('aaa-->onmessageerror')
}

parentPort.onerror = function (data) {
    console.log('aaa-->onerror')
}

function getRootData(data) {
    filemanager.getRoot()
        .then(file => {
            handleData(file, data)

        })
        .catch((error) => {
            console.log('aaa--getRoot 2 error' + error)
        });
}

function getListFile(data) {
    filemanager.listFile(data.path, 'file')
        .then(fileFolder => {
            if(Array.isArray(fileFolder)) {
                for (var i = 0; i < fileFolder.length; i++) {
                    var folder = fileFolder[i]
                    filemanager.listFile(folder.path, 'file')
                        .then(fileList => {
                            console.log('imageList1'+JSON.stringify(fileList))
                            if(Array.isArray(fileList)){
                                for(var j=0;j<fileList.length-1;j++){
                                    for(var i =0;i < fileList.length-1-j;i++){
                                        if(fileList[i].modified_time < fileList[i+1].modified_time){
                                            var flag = false
                                            var temp = fileList[i]
                                            fileList[i] = fileList[i+1]
                                            fileList[i+1] = temp
                                        }
                                    }
                                    if(flag){
                                        return
                                    }
                                }
                                for (var index = 0; index < fileList.length; index++) {
                                    var file = fileList[index]
                                    filemanager.listFile(data.path+file.path, data.media_type)
                                        .then(fileItem => {
                                            console.log('imageList2'+JSON.stringify(fileItem))
                                            handleData(fileItem, data)
                                        })
                                        .catch((error) => {
                                            console.log('aaa--fileListError' + error)
                                            handleData([], data)
                                        })
                                }
                            }
                        })
                        .catch((error) => {
                            console.log('aaa--fileError' + error)
                            handleData([], data)
                        })
                }
            }
        })
        .catch((error) => {
            console.log('aaa--getFileError' + error)
            handleData([], data)

        })
}

function handleData(file, data) {
    var info = JSON.stringify(file)
    var buf = new ArrayBuffer(info.length * 2)
    var bufView = new Uint16Array(buf)
    for (var index = 0; index < info.length; index++) {
        bufView[index] = info.charCodeAt(index)
    }
    console.log(data);
    parentPort.postMessage({ data: bufView, params: data })
}


